package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

public class CnforexNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://www.cnforex.com/.*/[0-9]+/[0-9]+/[0-9]+/[a-z0-9]{32}.html"
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		news.setUrl(url);
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
			title =doc.select("h1.h1Title").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div.divContent"));
			String text=doc.select("div.divInfo").text();
			date=DateUtils.matchDate(text);
//			news.setAuthor(StringUtils.substringAfter(text, "来源：").replace(" ", ""));
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setDate(date);

		return news;
	}

	public static void main(String[] args) {
		CnforexNewsAnalyse analyse=new CnforexNewsAnalyse();
		String url="http://www.cnforex.com/news/html/2014/2/25/4e1a855f522b3e65ba899268734dee03.html";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}

}
